﻿using System;
using Common.Enumerations;
using Sanelib.Common.Extensions;
using Sanelib.Common.Helpers;
using Sanelib.Dto;
using Sanelib.Dto.Enumerations;

namespace Common.Commands.MemberCommands
{
    public class UpdateMember : AuditedCommand
    {
        public string TagId { get; set; }
        public string Code { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public string ScreenMessage { get; set; }
        public DateTime? ValidFrom { get; set; }
        public DateTime? ValidTo { get; set; }
        public bool? SendSms { get; set; }
        public bool? SendEmail { get; set; }
        public bool? UsePrint { get; set; }
        public bool? Block { get; set; }
        public Role Role { get; set; }
        public UserStatus UserStatus { get; set; }

        public override Guid? GetAggregateId()
        {
            return null;
        }

        public override ErrorResult Validate()
        {
            var validationResult = new ErrorResult();

            if (string.IsNullOrWhiteSpace(Code))
                validationResult.AddError("Code", " should not be empty.");

            if (!string.IsNullOrWhiteSpace(Code) && Code.Length > 64)
                validationResult.AddError("Code", " entered exceeds the maximum length 64");

            if (!string.IsNullOrWhiteSpace(Name) && Name.Length > 256)
                validationResult.AddError("Name", " entered exceeds the maximum length 256");

            if (string.IsNullOrWhiteSpace(Name))
                validationResult.AddError("Name", " should not be empty.");

            if (DateOfBirth > DateTime.Now)
                validationResult.AddError("DateOfBirth", " is invalid.");

            if (!string.IsNullOrWhiteSpace(Address) && Address.Length > 256)
                validationResult.AddError("Address", " entered exceeds the maximum length 256");

            if (string.IsNullOrWhiteSpace(Address))
                validationResult.AddError("Address", " should not be empty.");

            if (!string.IsNullOrWhiteSpace(Email) && Email.Length > 64)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (Email.IsEmpty())
            {
                validationResult.AddError("Email", "should not be empty");
            }
            else
            {
                if (Email.IsNotEmpty() && Formatter.EmailId(Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            if (!string.IsNullOrWhiteSpace(Mobile) && Mobile.Length > 32)
                validationResult.AddError("Mobile", " entered exceeds the maximum length 32");

            if (string.IsNullOrWhiteSpace(Mobile))
                validationResult.AddError("Mobile number ", "should not be empty.");

            if (!string.IsNullOrWhiteSpace(ScreenMessage) && ScreenMessage.Length > 512)
                validationResult.AddError("Screen Message", " entered exceeds the maximum length 512");

            if (ValidFrom > DateTime.Now)
                validationResult.AddError("Valid From Date", " is invalid.");

            if (Role == null)
                validationResult.AddError("Role", " is invalid.");

            if (UserStatus == null)
                validationResult.AddError("User Status", " is invalid.");

            return validationResult;
        }
    }
}